/**
 * Created by sapaspire on 07-02-2015.
 */
//var myapp = angular.module('myapp', []);


angular.module('users', ['ui.bootstrap']);

/*
angular.module('users', []).service('sharedProperties', function () {
    //$scope.userall=[];
    $http({
        method:"GET",url:"/users/GetUsersList", data: {}
    }).success(function (data, status ) {
        alert(data);
        return (data);
    }).error(function (data, status ) {
        alert(status.toString());
    });
 });
*/

angular.module('users').controller('userslist', function ($scope,   $http, $modal, $log ) {
    //$scope.userall=sharedProperties;
    $scope.userall=[];
    $http({
        method:"GET",
        url:"/users/GetUsersList",
        data: {}
    }).success(function (data, status ) {
        $scope.userall=data;
    }).error(function (data, status ) {
        alert(status.toString());
    });

    $scope.editRow = function(id){
        alert(id);
    };
    $scope.deleteRow = function(id){
        alert(id);
    };

});



angular.module('users').controller('ModalUserCtrl', function ($scope, $http, $modal, $log ) {
    $scope.users = [];
    $scope.items = [];

    $scope.open = function (size) {

        var modalInstance = $modal.open({
            templateUrl: 'userContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };


});

angular.module('users').controller('ModalInstanceCtrl', function ($scope, $http, $modalInstance, items) {
    $scope.genderList = [
        { GenderId: 'MALE', Name: 'MALE' },
        { GenderId: 'FEMALE', Name: 'FEMALE' },
        { GenderId: 'OTHERS', Name: 'OTHERS' }
    ];

    $scope.typeList = [
        { TypeId: 'SALES MANAGER', Name: 'SALES MANAGER' },
        { TypeId: 'SALES MAN', Name: 'SALES MAN'  }
    ];

    $scope.openDate = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened = true;
    };
    $scope.dateOptions = {
        formatDate: 'dd-MM-yy',
        formatYear: 'yy',
        startingDay: 1
    };


    $scope.ok = function () {
        $scope.submitted = true;
        if (!$scope.userForm.$valid) {
            alert("Please correct errors!");
        } else {
             var userData =
             {
                 name: $scope.user.name,
                 gender : $scope.user.gender,
                 birthdate : $scope.user.birthday,
                 address : $scope.user.address,
                 email : $scope.user.email,
                 contactno : $scope.user.contactno,
                 type : $scope.user.type
             };
                $http({
                    method:"POST",
                    url:"/users/newUser",
                    data: userData
                }).success(function (data, status ) {
                    //$scope.persons = data; // assign  $scope.persons here as promise is resolved here
                    alert("save");
                    $scope.close(data);
                }).error(function (data, status ) {
                    //$scope.status = status;
                    alert(status.toString());
                });


        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

});

